<template>
  <el-form ref="formRef" :model="form" label-width="120px" size="default">
    <el-form-item>
      <h2>
        <el-link type="warning">在nav_tubeCode表中筛选试管 -----> 删除，id相同，_id不同的数据</el-link>
      </h2>
    </el-form-item>
    <el-form-item label="_id">
      <el-input v-model="form._id" clearable class="input-width"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="danger" @click="reset">重置</el-button>
    </el-form-item>
    <el-form-item label="">
      {{ JSON.stringify(form._ids) }}
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">生成脚本</el-button>
      <el-button type="default" @click="exportFile">导出文件</el-button>
    </el-form-item>
    <el-form-item label="脚本：">
      <el-input autosize type="textarea" v-model="form.scriptString" readonly></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive } from "vue";
import saveAs from "./js/FileSaver";
// do not use same name with ref
const form = reactive({
  // 要删除的id
  _id: "",
  // 集合
  _ids: [],
  // 脚本
  scriptString: ""
});

const add = () => {
  form._id.trim() ? form._ids.push(form._id.trim()) : "";
  form._id = "";
};

const reset = () => {
  form._id = "";
  form._ids = [];
};

const exportFile = () => {
  let str = new Blob([form.scriptString], {
    type: "text/plain;charset=utf-8"
  });
  saveAs(str, `修复重复id.txt`);
};

const onSubmit = () => {
  let code = form._ids.map((item) => {
    return `ObjectId('${item}')`;
  });
  form.scriptString = `环境：太极政务云
数据库地址：192.159.107.33:2000
数据库名：nat_admin

先备份：（请将备份的数据写在jira的备注里，如果数据多，可以生成txt文件上传到jira里）
db.nat_tube.find({"_id":{"$in":${JSON.stringify(code).replaceAll('"', "")}}});

再删除：
db.nat_tube.remove({"_id":{"$in":${JSON.stringify(code).replaceAll('"', "")}}});`;
};
</script>

<style>
.input-width {
  width: 500px;
}
</style>
